package com.fingard.dsp.bank.directbank.boc22;

import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.ITcpClient;
import com.fingard.net.TcpPortClient;
import com.fingard.net.TcpReceiver;

public class BOC22Base extends DirectBase implements ITcpClient {
    protected String getCharset() {
        return getBankConfig().getCharset("GBK");
    }

    protected String[] sendToBank(String p_reqStr, TcpPortClient tmpClient) throws Exception {
        String[] tmpStrRet = new String[]{"", ""};

        // 消息报文
        WriteBankLogLn("send p_reqStr len:" + p_reqStr.getBytes(getCharset()).length);
        String tmpReqMsgHeader = String.format("%04d",p_reqStr.getBytes(getCharset()).length);
        String tmpReqData = tmpReqMsgHeader + p_reqStr;
        WriteBankLogStep2(tmpReqData);
        TcpReceiver tmpReceiver = tmpClient.sendMessageClose(tmpReqData.getBytes(getCharset()));
        tmpStrRet[0] = tmpReceiver.connState;
        if (tmpReceiver.recBytes != null) {
            tmpStrRet[1] = new String(tmpReceiver.recBytes, 0, tmpReceiver.readedLength, getCharset());
        } else {
            tmpStrRet[1] = tmpReceiver.getConnMsg();
        }
        WriteBankLogStep3(tmpStrRet[1]);
        if (!tmpReceiver.isCompleted) {
            WriteBankLogLn(tmpReceiver.getConnMsg());
        }

        return tmpStrRet;
    }

    protected String[] sendToBank(byte[] reqBytes, TcpPortClient tmpClient) throws Exception {
        String[] tmpStrRet = new String[]{"", ""};

        // 消息报文
        WriteBankLogLn("send p_reqStr len:" + reqBytes.length);
        String tmpReqMsgHeader = String.format("%04d", reqBytes.length);
        WriteBankLogLn("tmpReqMsgHeader:" + tmpReqMsgHeader);
        byte[] reqHeader = tmpReqMsgHeader.getBytes(getCharset());
        byte[] tmpSendBytes = new byte[reqHeader.length + reqBytes.length];
        System.arraycopy(reqHeader, 0, tmpSendBytes, 0, reqHeader.length);
        System.arraycopy(reqBytes, 0, tmpSendBytes, reqHeader.length, reqBytes.length);

        String tmpReqData = tmpReqMsgHeader + new String(reqBytes, getCharset());
        WriteBankLogStep2(tmpReqData);

        TcpReceiver tmpReceiver = tmpClient.sendMessageClose(tmpSendBytes);
        tmpStrRet[0] = tmpReceiver.connState;
        if (tmpReceiver.recBytes != null) {
            tmpStrRet[1] = new String(tmpReceiver.recBytes, 0, tmpReceiver.readedLength, getCharset());
        } else {
            tmpStrRet[1] = tmpReceiver.getConnMsg();
        }
        WriteBankLogStep3(tmpStrRet[1]);
        if (!tmpReceiver.isCompleted) {
            WriteBankLogLn(tmpReceiver.getConnMsg());
        }

        return tmpStrRet;
    }

    @Override
    public void getContentLength(TcpReceiver p_receiver) throws Exception {
        if (p_receiver.contentLength == -1 && p_receiver.readedLength >= 4) {
            p_receiver.contentLength = Integer.parseInt(new String(p_receiver.recBytes, 0, 4, getCharset()).trim());
            p_receiver.contentStartIndex = 4;

        }
    }
}